package cn.pinming.config.shiro;

import cn.pinming.config.shiro.realm.DBAuthorizationRealm;
import com.google.common.collect.Maps;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.DefaultFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * Shiro 配置
 *
 * @author wuwanggao
 */
@Configuration
public class ShiroConfiguration {

    /**
     * 密码加密
     *
     * @return
     */
    @Bean
    public PmCredentialsMatcher getCredentialsMatcher() {
        return new PmCredentialsMatcher();
    }

    /**
     * 登陆和权限配置
     *
     * @return
     */
    @Bean
    public AuthorizingRealm getRealm() {
        DBAuthorizationRealm realm = new DBAuthorizationRealm();
        realm.setCredentialsMatcher(getCredentialsMatcher());
        return realm;
    }

    /**
     * 安全管理器
     *
     * @return
     */
    @Bean
    public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(getRealm());
        return securityManager;
    }

    /**
     * 资源拦截器
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setLoginUrl("/login/index");
        shiroFilterFactoryBean.setSuccessUrl("/index");
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");
        //需要用LinkedHashMap不能使用HashMap相关结构
        Map<String, String> filterChainDefinitionMap = Maps.newHashMap();
        filterChainDefinitionMap.put("/webjars/**", DefaultFilter.anon.name());
        filterChainDefinitionMap.put("/static/**", DefaultFilter.anon.name());
        filterChainDefinitionMap.put("/favicon.ico", DefaultFilter.anon.name());
        filterChainDefinitionMap.put("/register", DefaultFilter.anon.name());
        filterChainDefinitionMap.put("/login/signIn", DefaultFilter.anon.name());
        filterChainDefinitionMap.put("/logout", DefaultFilter.logout.name());

        filterChainDefinitionMap.put("/**", DefaultFilter.authc.name());
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        return shiroFilterFactoryBean;
    }

    /**
     * 缓存管理
     *
     * @return
     */
    @Bean
    public CacheManager cacheManager() {
        EhCacheManager cacheManager = new EhCacheManager();
        return cacheManager;
    }

    /**
     * 注解配置
     *
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

}
